/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hellfire.platform.graphics;

import hellfire.system.engine.Main;
import hellfire.system.graphics.*;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Log;

public class GLProgressBar extends GLImage
{
	private float progress = 0f;
	private float[] progressColor = GLColor.WHITE.asFloatArray();
	
    public GLProgressBar(int resource, GLVec2 pos, GLVec2 dim) 
    {
    	super(resource, pos, dim);
    	program = Main.UI.renderer.getShaderProgram(PlatformShaders.pBar);
    }
   
    public void setProgressColor(GLColor c)
    {
    	this.progressColor = c.asFloatArray();
    }
    
    public GLColor getProgressColor()
    {
    	return new GLColor(this.progressColor);
    }
    
    public void setProgress(float progress)
    {
    	this.progress = progress;
    }
    
    public float getProgress()
    {
    	return progress;
    }
    
    @Override
    public void draw(float[] matrix) 
    {
    	int mPositionHandle;
    	int mTexelHandle;
        int mColorHandle;
        int mBarColorHandle;
        int mProgressHandle;
        int mMVPMatrixHandle;
        int mTextureHandle;
        int texture;
        
        Main.UI.renderer.pushMatrix(matrix);
        Matrix.translateM(matrix, 0, pos.x(), pos.y(), 0);
        Matrix.rotateM(matrix, 0, rotation, 0, 0, 1);
    	Matrix.scaleM(matrix, 0, matrix, 0, dim.x(), dim.y(), 1);
        GLES20.glUseProgram(program);
    	
    	if((texture = cache.get(0)) < 0)
        	texture = cache.set(0, Main.UI.renderer.ResourceIdToTexture(resourceId));
    	
        if((mPositionHandle = cache.get(1)) < 0)
        	mPositionHandle = cache.set(1, GLES20.glGetAttribLocation(program, "aPosition"));
        
        if((mColorHandle = cache.get(2)) < 0)
        	mColorHandle = cache.set(2, GLES20.glGetUniformLocation(program, "uColor"));
        
        if((mTexelHandle = cache.get(3)) < 0)
        	mTexelHandle = cache.set(3, GLES20.glGetAttribLocation(program, "aTexCoord"));
        
        if((mTextureHandle = cache.get(4)) < 0)
        	mTextureHandle = cache.set(4, GLES20.glGetUniformLocation(program, "uTexture"));
        
        if((mMVPMatrixHandle = cache.get(5)) < 0) 
        	mMVPMatrixHandle = cache.set(5, GLES20.glGetUniformLocation(program, "uMVPMatrix"));
        
        if((mBarColorHandle = cache.get(6)) < 0)
        	mBarColorHandle = cache.set(6, GLES20.glGetUniformLocation(program, "uProgressColor"));
        
        if((mProgressHandle = cache.get(7)) < 0)
        	mProgressHandle = cache.set(7, GLES20.glGetUniformLocation(program, "uProgress"));
        
        // Enable a handle to the triangle vertices
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        // Prepare the triangle coordinate data
        GLES20.glVertexAttribPointer(
                mPositionHandle, 3,
                GLES20.GL_FLOAT, false,
                12, GLRect.vertexBuffer);
        
        GLES20.glEnableVertexAttribArray(mTexelHandle);
        GLES20.glVertexAttribPointer(
        		mTexelHandle, 2,
                GLES20.GL_FLOAT, false,
                0, getTexCoordBuffer());
        
        GLES20.glUniform4fv(mColorHandle, 1, maskColor, 0);
        
        GLES20.glUniform4fv(mBarColorHandle, 1, this.progressColor, 0);
        
        GLES20.glUniform1f(mProgressHandle, progress);
        
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
        GLES20.glUniform1i(mTextureHandle, 0);
        
        // Apply the projection and view transformation
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, matrix, 0);
        //GLRenderer.checkGLError("glUniformMatrix4fv");

        // Draw the square
        GLES20.glDrawElements(
                GLES20.GL_TRIANGLES, GLRect.drawOrder.length,
                GLES20.GL_UNSIGNED_SHORT, GLRect.drawListBuffer);

        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mTexelHandle);
    	
        Main.UI.renderer.popMatrix(matrix);
    }
    
    

}